package cn.seaboot.admin.starter.upload;

import cn.seaboot.admin.upload.Uploader;
import cn.seaboot.admin.upload.LocalStorage;
import cn.seaboot.commons.file.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.File;

/**
 * 上传文件的最大值等配置，系统中的文件上传配置，是经过重新配置的
 *
 * @author Mr.css
 * @version 2020-03-06 10:16
 */
@Configuration
public class UploadConfiguration {
    private final Logger logger = LoggerFactory.getLogger(UploadConfiguration.class);

    @Resource
    private UploadProperties uploadProperties;

    /**
     * 文件上传处理器，文件大小限制等
     *
     * @return MultipartResolver
     */
    @Bean
    public MultipartResolver multipartResolver() {
        CommonsMultipartResolver resolver = new CommonsMultipartResolver();
        resolver.setDefaultEncoding("UTF-8");
        // 是否等到使用的时候才解析 request
        resolver.setResolveLazily(uploadProperties.isResolveLazily());
        resolver.setMaxInMemorySize(uploadProperties.getMaxInMemorySize());
        resolver.setMaxUploadSize(uploadProperties.getMaxUploadSize());
        return resolver;
    }

    /**
     * 将下列这些文件单独做配置，主要是因为需要各自专门的代码，
     * 如果不区分文件类型，使用 file 一种即可。
     */
    @PostConstruct
    public void initStorage() {
        if (uploadProperties.getRoot() == null) {
            // 默认使用用户目录
            uploadProperties.setRoot(FileUtils.getUserDirectoryPath());
        }
        logger.info("[--- Configuration ---]Upload file:{}", uploadProperties.getRoot());

        // 文件是特殊的，只需要调整代码逻辑，可以指代下列全部
        if (uploadProperties.getFilePath() == null) {
            uploadProperties.setFilePath(uploadProperties.getRoot() + File.separator + StorageName.FILE);
        }

        // 专有文件，每一种都包含各自特有的处理程序
        if (uploadProperties.getAppPath() == null) {
            uploadProperties.setAppPath(uploadProperties.getRoot() + File.separator + StorageName.APP);
        }
        if (uploadProperties.getWordPath() == null) {
            uploadProperties.setWordPath(uploadProperties.getRoot() + File.separator + StorageName.WORD);
        }
        if (uploadProperties.getExcelPath() == null) {
            uploadProperties.setExcelPath(uploadProperties.getRoot() + File.separator + StorageName.EXCEL);
        }
        if (uploadProperties.getImagePath() == null) {
            uploadProperties.setImagePath(uploadProperties.getRoot() + File.separator + StorageName.IMAGE);
        }

        Uploader.createStorage(StorageName.IMAGE, new LocalStorage(new File(uploadProperties.getImagePath())));
        Uploader.createStorage(StorageName.EXCEL, new LocalStorage(new File(uploadProperties.getExcelPath())));
        Uploader.createStorage(StorageName.WORD, new LocalStorage(new File(uploadProperties.getWordPath())));
        Uploader.createStorage(StorageName.APP, new LocalStorage(new File(uploadProperties.getAppPath())));
        Uploader.createStorage(StorageName.FILE, new LocalStorage(new File(uploadProperties.getFilePath())));
    }
}
